const std = @import("std");
const root = @import("kernel.zig");

const print = root.print;

extern var _heap_start: u8; // 在 ld 中定义的这些只是 symbol，不占空间
extern const _heap_size: u8; // 值其实体现在它们的地址上：heap_size == &_heap_size
extern const _text_start: u8;
extern const _text_end: u8;
extern const _rodata_start: u8;
extern const _rodata_end: u8;
extern const _data_start: u8;
extern const _data_end: u8;
extern const _bss_start: u8;
extern const _bss_end: u8;

pub var fba: std.heap.FixedBufferAllocator = undefined;

pub fn init() void {
    const heap_start_ptr: [*]u8 = @ptrCast(&_heap_start);
    const heap_slice = heap_start_ptr[0..@intFromPtr(&_heap_size)];
    // fba 必须是全局变量，不然函数返回之后就没了
    fba = .init(heap_slice);
}

pub fn info() void {
    print(
        \\TEXT:   0x{x} -> 0x{x}
        \\RODATA: 0x{x} -> 0x{x}
        \\DATA:   0x{x} -> 0x{x}
        \\BSS:    0x{x} -> 0x{x}
        \\HEAP:   0x{x} -> 0x{x}
        \\HEAP_SIZE = 0x{x:0>8}, num of pages = {d}
        \\
    , .{
        @intFromPtr(&_text_start),
        @intFromPtr(&_text_end),
        @intFromPtr(&_rodata_start),
        @intFromPtr(&_rodata_end),
        @intFromPtr(&_data_start),
        @intFromPtr(&_data_end),
        @intFromPtr(&_bss_start),
        @intFromPtr(&_bss_end),
        @intFromPtr(&_heap_start),
        @intFromPtr(&_heap_start) + @intFromPtr(&_heap_size),
        @intFromPtr(&_heap_size),
        @intFromPtr(&_heap_size) / 4096,
    });
}
